#pragma once
#include <string>
#include "Util.hpp"
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
const std::string sep = "\r\n";
const std::string default_root = "./wwwroot";
const std::string home_page = "index.html";
const std::string html_404 = "wwwroot/404.html";
// 请求
class HttpRequest
{
public:
    HttpRequest()
    {
    }
    void parse()
    {
        // 获取到一行的数据了
        std::string line = Util::getOneLine(inbuffer, sep);
        if (line.empty())
        {
            return;
        }
        // 接着按照空格分割字符串。用一种新的方法，会直接按照空格划分出来。
        std::stringstream ss(line);
        ss >> method >> url >> httpversion;

        path = default_root; // ./wwwroot/
        path += url;
        if (path[path.size() - 1] == '/')
        {
            path += home_page;
        }
        auto pos = path.rfind(".");
        if (pos == std::string::npos)
        {
            suffix = ".html";
        }
        else
        {
            suffix = path.substr(pos);
        }
        struct stat st;
        int n = stat(path.c_str(), &st);
        if (n == 0)
            size = st.st_size;
        else
            size = -1;
    }

public:
    std::string inbuffer;
    std::string method;
    std::string url;
    std::string httpversion;
    std::string path;
    std::string suffix; // 后缀
    int size;
};

// 相应
class HttpResponse
{
public:
    HttpResponse()
    {
    }

public:
    std::string outbuffer;
};